package org.jeecg.modules.bus.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.bus.entity.BusClientPersonVO;
import org.jeecg.modules.bus.entity.BusContactPerson;
import org.jeecg.modules.bus.service.IBusContactPersonService;
import org.jeecg.modules.bus.entity.BusClientData;
import org.jeecg.modules.bus.service.IBusClientDataService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 商务管理(客户管理)数据表
 * @Author: jeecg-boot
 * @Date:   2022-11-03
 * @Version: V1.0
 */
@Api(tags="商务管理(客户管理)数据表")
@RestController
@RequestMapping("/bus/busClientData")
@Slf4j
public class BusClientDataController extends JeecgController<BusClientData, IBusClientDataService> {
	@Autowired
	private IBusClientDataService busClientDataService;
	@Autowired
	private IBusContactPersonService busContactPersonService;

	/**
	 * 分页列表查询
	 *
	 * @param busClientData
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "商务管理(客户管理)数据表-分页列表查询")
	@ApiOperation(value="商务管理(客户管理)数据表-分页列表查询", notes="商务管理(客户管理)数据表-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="bus/BusClientList")
	public Result<?> queryPageList(BusClientData busClientData,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusClientData> queryWrapper = QueryGenerator.initQueryWrapper(busClientData, req.getParameterMap());
		Page<BusClientData> page = new Page<BusClientData>(pageNo, pageSize);
		IPage<BusClientData> pageList = busClientDataService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param busClientData
	 * @return
	 */
	@AutoLog(value = "商务管理(客户管理)数据表-添加")
	@ApiOperation(value="商务管理(客户管理)数据表-添加", notes="商务管理(客户管理)数据表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BusClientData busClientData) {
		busClientDataService.save(busClientData);
		return Result.OK("添加成功！");
	}

	 /**
	  *   添加添加客户与联系人
	  *
	  * @param busClientPersonVO
	  * @return
	  */
	 @AutoLog(value = "商务管理(客户管理)数据表-添加客户与联系人")
	 @ApiOperation(value="商务管理(客户管理)数据表-添加客户与联系人", notes="商务管理(客户管理)数据表-添加客户与联系人")
	 @PostMapping(value = "/addClientPerson")
	 public Result<?> addClientPerson(@RequestBody BusClientPersonVO busClientPersonVO) {
		 busClientDataService.save(busClientPersonVO.getBusClientData());
		 String id = busClientPersonVO.getBusClientData().getId();
		 List<BusContactPerson> busContactPersonList = busClientPersonVO.getBusContactPersonList();
		 for (BusContactPerson busContactPerson : busContactPersonList){
			 busContactPerson.setBusClientId(id);
			 busContactPersonService.save(busContactPerson);
		 }
		 return Result.OK("添加成功！");
	 }

	/**
	 *  编辑
	 *
	 * @param busClientPersonVO
	 * @return
	 */
	@AutoLog(value = "商务管理(客户管理)数据表-编辑")
	@ApiOperation(value="商务管理(客户管理)数据表-编辑", notes="商务管理(客户管理)数据表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BusClientPersonVO busClientPersonVO) {
		String unifiedCreditCode = busClientPersonVO.getBusClientData().getUnifiedCreditCode();
		QueryWrapper<BusClientData> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("id").eq("unified_credit_code",unifiedCreditCode);
		List<BusClientData> busClientDataList = busClientDataService.list(queryWrapper);

		if (busClientDataList.size() != 0) {
			String id = busClientDataList.get(0).getId();
			if (!id.equals(busClientPersonVO.getBusClientData().getId())) {
				return Result.error("统一信用代码已存在,请重新输入");
			}
		}
		busClientDataService.updateById(busClientPersonVO.getBusClientData());
		String client_id = busClientPersonVO.getBusClientData().getId();
		List<BusContactPerson> busContactPersonList = busClientPersonVO.getBusContactPersonList();
		for (BusContactPerson busContactPerson : busContactPersonList){
			busContactPerson.setBusClientId(client_id);
			busContactPersonService.save(busContactPerson);
		}
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理(客户管理)数据表-通过id删除")
	@ApiOperation(value="商务管理(客户管理)数据表-通过id删除", notes="商务管理(客户管理)数据表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		busClientDataService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商务管理(客户管理)数据表-批量删除")
	@ApiOperation(value="商务管理(客户管理)数据表-批量删除", notes="商务管理(客户管理)数据表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busClientDataService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理(客户管理)数据表-通过id查询")
	@ApiOperation(value="商务管理(客户管理)数据表-通过id查询", notes="商务管理(客户管理)数据表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BusClientData busClientData = busClientDataService.getById(id);
		if(busClientData==null) {
			return Result.error("未找到对应数据");
		}
		QueryWrapper<BusContactPerson> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("bus_client_id",id);
		List<BusContactPerson> busContactPersonList = busContactPersonService.list(queryWrapper);
		BusClientPersonVO busClientPersonVO = new BusClientPersonVO();
		busClientPersonVO.setBusClientData(busClientData);
		busClientPersonVO.setBusContactPersonList(busContactPersonList);

		return Result.OK(busClientPersonVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param busClientData
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusClientData busClientData) {
        return super.exportXls(request, busClientData, BusClientData.class, "商务管理(客户管理)数据表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusClientData.class);
    }

}
